<style>
    .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {height:34px;line-height:34px;padding:0 8px;}
</style>
<body>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <div >
            <table id="model-table" class="layui-table" lay-filter="model-table"></table>
        </div>
    </div>
</div>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
    </div>
</script>
<!-- 状态列 -->
<script type="text/html" id="set">
    <a class="layui-btn layui-btn-primary layui-btn-xs">字段管理</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs">内容管理</a>
</script>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var miniPage = layui.miniPage;

        // 渲染表格
        layer.load(2);
        table.render({
            elem: '#model-table',
            url: 'model/list',
            toolbar: '#toolbarDemo',
            title: '模型数据表',
            page: true,
            limit: 10,
            cols: [[
                {type: 'numbers'},
                {field: 'name', title: '名称'},
                {field: 'table_name', title: '数据库表名'},
                {field: 'description', title: '描述'},
                {field: 'created_at', title: '添加时间'},
                {field: 'updated_at', title: '更新时间'},
                {templet: '#set', title:'管理', width:200, align:'center',unresize:true},
                {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        //监听工具条
        table.on('tool(model-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                layer.confirm('真的删除这个菜单吗', function (index) {
                    $.ajax({
                        url:'model/'+data.id,
                        method: 'DELETE',
                        success: function (res) {
                            layer.msg(res.msg,function () {
                                obj.del();
                                layer.close(index);
                            })
                        }
                    })
                });
            } else if (layEvent === 'edit') {
                // layer.msg('修改' + data.id);
                var content = miniPage.getHrefContent('model/'+data.id+'/edit');
                var openWH = miniPage.getOpenWidthHeight();
                var edit_view = layer.open({
                    title: '修改菜单',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(edit_view);
                });
            }
        });
        /**
         * toolbar事件监听
         */
        table.on('toolbar(model-table)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('model/create');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '添加模型',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                // 删除操作
            }
        });
    });
</script>
</body>
